package com.min.license;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.asymmetric.RSA;
import com.min.license.config.LicenseProperties;
import com.min.license.entity.License;
import com.min.license.util.SystemUtil;

public class MainApplication {

    public static void main(String[] args) {
        if (args == null || args.length != 1) {
            System.err.println("参数错误，需要一个天数参数，eg: java -jar min-license.jar 30");
        } else {
            final long days = Long.parseLong(args[0]);
            String id = SystemUtil.getId();
            if (StrUtil.isBlank(id)) {
                System.err.println("获取机器唯一码失败");
            } else {
                System.out.println("机器标识:" + id);
                RSA rsa = new RSA();
                final License license = new License(id, days);
                final LicenseManager licenseManager = new LicenseManager(new LicenseProperties());
                final byte[] secret = licenseManager.encode(license, rsa.getPublicKeyBase64(), rsa.getPrivateKeyBase64());
                final String property = System.getProperty("user.dir");
                FileUtil.writeBytes(secret, property + "/license");
                System.out.println("【创建成功】许可文件位置：" + property);
            }
        }
    }

}
